package com.agileboot.domain.bot.conversationRecord;

import com.agileboot.common.core.page.PageDTO;
import com.agileboot.domain.bot.conversationRecord.command.AddConversationRecordCommand;
import com.agileboot.domain.bot.conversationRecord.command.UpdateConversationRecordCommand;
import com.agileboot.domain.bot.conversationRecord.db.ConversationRecordEntity;
import com.agileboot.domain.bot.conversationRecord.db.ConversationRecordService;
import com.agileboot.domain.bot.conversationRecord.dto.ConversationRecordDTO;
import com.agileboot.domain.bot.conversationRecord.model.ConversationRecordModel;
import com.agileboot.domain.bot.conversationRecord.model.ConversationRecordModelFactory;
import com.agileboot.domain.bot.conversationRecord.query.ConversationRecordQuery;
import com.agileboot.domain.common.command.BulkOperationCommand;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class ConversationRecordApplicationService {

    private final ConversationRecordService conversationRecordService;
    private final ConversationRecordModelFactory conversationRecordModelFactory;

    public PageDTO<ConversationRecordDTO> getConversationRecordList(ConversationRecordQuery query) {
        Page<ConversationRecordEntity> page = conversationRecordService.page(query.toPage(), query.toQueryWrapper());
        List<ConversationRecordDTO> records = page.getRecords().stream().map(ConversationRecordDTO::new).collect(Collectors.toList());
        return new PageDTO<>(records, page.getTotal());
    }

    public List<ConversationRecordDTO> getConversationRecordListAll(ConversationRecordQuery query) {
        List<ConversationRecordEntity> all = conversationRecordService.list(query.toQueryWrapper());
        return all.stream().map(ConversationRecordDTO::new).collect(Collectors.toList());
    }

    public ConversationRecordDTO getConversationRecordInfo(Long id) {
        ConversationRecordEntity byId = conversationRecordService.getById(id);
        return new ConversationRecordDTO(byId);
    }

    public void addConversationRecord(AddConversationRecordCommand command) {
        ConversationRecordModel model = conversationRecordModelFactory.create();
        model.loadFromAddCommand(command);
        model.insert();
    }

    public void updateConversationRecord(UpdateConversationRecordCommand command) {
        ConversationRecordModel model = conversationRecordModelFactory.loadById(command.getId());
        model.loadFromUpdateCommand(command);
        model.updateById();
    }

    public void deleteConversationRecord(BulkOperationCommand<Long> deleteCommand) {
        conversationRecordService.removeBatchByIds(deleteCommand.getIds());
    }
}
